package com.lineage.server.clientpackets;

import com.lineage.echo.ClientExecutor;
import com.lineage.list.PcLvSkillList;
import com.lineage.server.datatables.lock.CharSkillReading;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Object;
import com.lineage.server.serverpackets.S_NPCTalkReturn;
import com.lineage.server.serverpackets.S_SkillBuy;
import com.lineage.server.serverpackets.S_SkillBuyCN;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_SkillBuy extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_SkillBuy.class);
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      read(decrypt);
      L1PcInstance pc = client.getActiveChar();
      if (!pc.isGhost() && 
        !pc.isDead() && 
        !pc.isTeleport() && 
        !pc.isPrivateShop()) {
        int objid = readD();
        L1Object obj = World.get().findObject(objid);
        if (obj == null)
          return; 
        L1NpcInstance npc = null;
        if (obj instanceof L1NpcInstance)
          npc = (L1NpcInstance)obj; 
        if (npc == null)
          return; 
        if (npc.getNpcId() == 70754) {
          pc.get_other().set_shopSkill(true);
          pc.sendPackets((ServerBasePacket)new S_SkillBuyCN(pc, npc));
          return;
        } 
        pc.get_other().set_shopSkill(false);
        ArrayList<Integer> skillList = PcLvSkillList.scount(pc);
        ArrayList<Integer> newSkillList = new ArrayList<>();
        Iterator<Integer> iterator = skillList.iterator();
        while (iterator.hasNext()) {
          Integer integer = iterator.next();
          if (!CharSkillReading.get().spellCheck(pc.getId(), integer.intValue() + 1))
            newSkillList.add(integer); 
        } 
        switch (npc.getNpcId()) {
          case 70009:
            if (pc.getLawful() < 0) {
              pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "gerengEv2"));
              return;
            } 
            if (newSkillList.size() <= 0) {
              pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "gerengEv3"));
              return;
            } 
            pc.sendPackets((ServerBasePacket)new S_SkillBuy(pc, newSkillList));
            return;
        } 
        pc.sendPackets((ServerBasePacket)new S_SkillBuy(pc, newSkillList));
        return;
      } 
      return;
    } catch (Exception exception) {
    
    } finally {
      over();
    } 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
